#!C:/Perl/bin/perl.exe -w
use DBI;
use strict;

# Open the repository (SQLite database).
my $dbfile = 'aleks.sqlite';  # in the current directory
my $db = DBI->connect("dbi:SQLite:dbname=$dbfile","","") or die $DBI::errstr;
my $stmt;  # a temporary SQL statement holder
my @row;  # a temporary row holder

my $sql = 'select name from student;';
my $stmt = $db->prepare($sql) or die $db->errstr;
$stmt->execute() or die $stmt->errstr;
while (@row = $stmt->fetchrow_array) {
    my $fullName = $row[0];
    if ($fullName =~ / (\S+)$/) {
        print $fullName, ' --> ', $1, "\n";
        my $sql = 'update student set lastName = ? where name = ?';
        $db->do($sql, undef, $1, $fullName) or die $db->errstr;
    }
    else {
        print 'ERROR: no last name for ', $fullName, "\n";
    }
}
# Disconnect from the database
$db->disconnect;
